package com.is.control;

import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

import com.is.dbconn.DBConnect;
import com.is.model.ParamStuyearType;;

public class ParamStuyearTypeControl {
	
	public static ParamStuyearType getParamStuyearType(int id) {
		DBConnect dbConnect = new DBConnect();
		String sql = "select id from param_stuyear_type where id = '"+id+"'";
		dbConnect.prepareStatement(sql);
		ResultSet rs = null;
		ParamStuyearType pst = null;;
		try {
			rs = dbConnect.executeQuery();
			if(rs.next()){
				pst = new ParamStuyearType();
			    pst.setId(rs.getInt("id"));
			    pst.setTypeName(rs.getString("typeName"));
			    pst.setNote(rs.getString("note"));
			}
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			System.out.println("isInsert ERROR");
		}finally{
			dbConnect.close();
		}
		return pst;
	}
	
	public static String getTypeName(int id) {
		DBConnect dbConnect = new DBConnect();
		String sql = "select typeName from param_stuyear_type where id = '"+id+"'";
		dbConnect.prepareStatement(sql);
		ResultSet rs = null;
		try {
			rs = dbConnect.executeQuery();
			if(rs.next()){
			     return rs.getString("typeName");
			}
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			System.out.println("isInsert ERROR");
		}finally{
			dbConnect.close();
		}
		return null;
	}
	
	public static boolean insert(ParamStuyearType pst) {
		DBConnect dbConnect = new DBConnect();
		String sql = "insert into param_stuyear_type(typeName,note) values(?,?)";
		boolean flag = false;
		dbConnect.prepareStatement(sql);
		try {
			dbConnect.setString(1, pst.getTypeName());
			dbConnect.setString(2, pst.getNote());
			flag = dbConnect.executeUpdate();
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			System.out.println("insertPST　ERROR");
		}finally{
			dbConnect.close();
		}
		return flag;
	}
	

	public static List<ParamStuyearType> getAllStuyearTypes() {
		List<ParamStuyearType> types = new ArrayList<ParamStuyearType>();
		DBConnect dbConnect = new DBConnect();
		String sql = "select * from param_stuyear_type";
		ResultSet rSet = null;
		dbConnect.prepareStatement(sql);
		try {
			rSet = dbConnect.executeQuery();
			while(rSet.next()){
				ParamStuyearType ps = new ParamStuyearType();
				ps.setId(rSet.getInt("id"));
				ps.setNote(rSet.getString("note"));
				ps.setTypeName(rSet.getString("typeName"));
				types.add(ps);
			}
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}finally{
			dbConnect.close();
		}
		return types;
	}
	
}
